-
Couldn't load subscription status.
- Fork 768
Add support for custom per-field attributes #3307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jsgf
wants to merge
2
commits into
rust-lang:main
Choose a base branch
from
jsgf:custom-field-attrs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12b3e2a to
6554574
Compare
This adds the ability to apply custom Rust attributes to individual
struct, union, and newtype fields in generated bindings through three
mechanisms:
1. HTML annotations in C/C++ comments:
```c
/// <div rustbindgen attribute="serde(rename = x_coord)"></div>
int x;
```
2. ParseCallbacks::field_attributes() method for programmatic control:
```rust
fn field_attributes(&self, info: &FieldAttributeInfo) -> Vec<String>
```
3. CLI flag and Builder API for pattern-based attributes:
```bash
--field-attr "Point::x=serde(rename = x_coord)"
```
```rust
.field_attribute("Point", "x", "serde(rename = x_coord)")
```
All three mechanisms can be used together, with attributes merged in
order: annotations, callbacks, then CLI/Builder patterns.
This is useful for adding serde attributes, documentation, or other
derive-related metadata to specific fields.
6554574 to
5b9816f
Compare
meta-codesync bot
pushed a commit
to facebook/sapling
that referenced
this pull request
Oct 23, 2025
Summary: This includes 3 recent PRs: - rust-lang/rust-bindgen#3305 - Fix packed structs with flexible array members (merged) - rust-lang/rust-bindgen#3306 - Support nested flexible array members - rust-lang/rust-bindgen#3307 - Add support for custom per-field attributes The last one is specifically intended to allow per-field attributes from proc-macros to be applied. These have been cherry-picked onto the v0.72.1 release branch, as the main branch is still listed as v0.72.0 and I didn't want a backwards version step. The upstream branch is https://github.com/jsgf/rust-bindgen/tree/PR-merged Reviewed By: dtolnay Differential Revision: D85273915 fbshipit-source-id: 211916b9611cbfa409296a6854b4a94bba2c5195
meta-codesync bot
pushed a commit
to facebookexperimental/rust-shed
that referenced
this pull request
Oct 23, 2025
Summary: This includes 3 recent PRs: - rust-lang/rust-bindgen#3305 - Fix packed structs with flexible array members (merged) - rust-lang/rust-bindgen#3306 - Support nested flexible array members - rust-lang/rust-bindgen#3307 - Add support for custom per-field attributes The last one is specifically intended to allow per-field attributes from proc-macros to be applied. These have been cherry-picked onto the v0.72.1 release branch, as the main branch is still listed as v0.72.0 and I didn't want a backwards version step. The upstream branch is https://github.com/jsgf/rust-bindgen/tree/PR-merged Reviewed By: dtolnay Differential Revision: D85273915 fbshipit-source-id: 211916b9611cbfa409296a6854b4a94bba2c5195
emilio
reviewed
Oct 24, 2025
fa7c579 to
f2ef849
Compare
A single function gathers custom field attributes from cli, API and annotations, and then use it for both newtype and regular struct/union fields.
f2ef849 to
943d01a
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds the ability to apply custom Rust attributes to individual
struct, union, and newtype fields in generated bindings through three
mechanisms:
--field-attr "Point::x=serde(rename = x_coord)"All three mechanisms can be used together, with attributes merged in
order: annotations, callbacks, then CLI/Builder patterns.
This is useful for adding serde attributes, documentation, or other
derive-related metadata to specific fields.